<?php 
//I.F. Nuevo: 2011-02-28; ramarin
require_once('../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/report/moosisca/workflowlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/report/moosisca/managecourseslib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/report/moosisca/courseapprovation_form.php');
require_once($CFG->dirroot . '/course/lib.php');

global $USER;

$id = required_param('id', PARAM_INT); 

admin_externalpage_setup('moosisca_unapprovedcourses');
admin_externalpage_print_header();
echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/report/moosisca/styles.php" />';

$mform_approvation = new courseapprovation_form();

if ($frm = data_submitted() and confirm_sesskey()) {

    $workflow = get_records('moosisca_workflow', 'id', $id);
  
    if(!empty($workflow)){      
        $workflow = $workflow[$id];
        $state = $workflow->state;    
        $workflow->state = empty($frm->btnApprove) ? 'observations' : 'approved';

        if(update_record('moosisca_workflow', $workflow)){
            $history = new stdClass();
            $history->workflowid = $workflow->id;
            $history->state = empty($frm->btnApprove) ? 'observations' : 'approved';
            $history->userid = $USER->id;
            $history->observations = $frm->observations;
            $history->timecreated = time();

            $dir = observations_file_area_name($workflow->id);

            if($observationid = insert_record('moosisca_workflow_history', $history)){
                $success = 1;
                $message = '';
              
                require_once($CFG->dirroot.'/lib/uploadlib.php');
                $um = new upload_manager("attachment",false,true,null,false,0,false,true);
                if ($um->process_file_uploads($dir)) {
                    $message .= $um->get_errors();
                    $newfilename = $um->get_new_filename();
                }
                $message .= $um->get_errors();

                if(!empty($message)){
                    notice(get_string('approvedcoursefailed', 'report_moosisca') . '<br />' . $message);
                }
                else{
                    set_field("moosisca_workflow_history", "attachment", $newfilename, "id", $observationid);
                    
                    if(isset($frm->mailnow) && $frm->mailnow){                          
                        $user = get_records('user', 'id', $workflow->userid);
                        
                        if($user){
                            $user = $user[$workflow->userid];
                            send_observation_email($id, $user, $newfilename);                            
                        }
                    }
                }
            }
            else{
                $workflow->state = $state;
                update_record('moosisca_workflow', $workflow);
            }
        }
        else {
            notice(get_string('approvedcoursefailed', 'report_moosisca'));
        }
    }
    else{
        notice(get_string('approvedcoursefailed', 'report_moosisca'));
    }
                    
    if(!empty($success)){
        notice(get_string('successfulloperation', 'report_moosisca'), 'unapprovedcourses.php');
    }
}

$systemcontext = get_context_instance(CONTEXT_SYSTEM);

echo '<fieldset class="approve_form">';

$mform_approvation->display();

$observations = get_observations_course("mw.id, mwh.observations, mwh.userid, concat(u.firstname, ' ', u.lastname) realname, mwh.attachment, mwh.timecreated ", "mw.id= '" . $id . "' AND mwh.state='observations'");

if($observations) {
    print_observations($observations);
}

echo "</fieldset>";

admin_externalpage_print_footer();

function send_observation_email($id, $user, $filename) {
    global $CFG;

    $site = get_site();
    $supportuser = generate_email_supportuser();

    $data = new object();
    $data->firstname = fullname($user);
    $data->admin = generate_email_signoff();

    $subject = get_string('emailobservationsubject', 'report_moosisca', format_string($site->fullname));

    $data->link = $CFG->wwwroot .'/admin/report/moosisca/viewobservations.php?id='. $id . '&amp;inpopup=0';
    $message     = get_string('emailobservation', 'report_moosisca', $data);
    $messagehtml = text_to_html(get_string('emailobservation', 'report_moosisca', $data), false, false, true);

    $user->mailformat = 1;  // Always send HTML version as well

    return email_to_user($user, $supportuser, $subject, $message, $messagehtml);
}